Imports System.Data

'TODO: Add Datatable viewstates to other forms
'TODO: Navigation from search

Partial Class WebForms_SearchReferrals
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SecurePage()
        Dim sec As New Secure

        With sec
            .ParentPage = ParentPage
            .AddControl(Print_btn, Secure.ControlTypes.PrintType)
            .AddControl(Save_btn, Secure.ControlTypes.PrintType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
            .AddDataGridColumn(DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
            .SetPageAccess(Session("UserName"))
        End With

        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call SecurePage()

        Select Case PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then Call SearchReferral()
    End Sub

    Protected Sub Search_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_btn.Click
        Call SearchReferral()
    End Sub

    Protected Sub Search_ClientIssues_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_ClientIssues.Click
        Response.Redirect("~/" & strPath & "/Search_CIssues.aspx")
    End Sub

    Protected Sub Search_Policies_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Search_Policies.Click
        Response.Redirect("~/" & strPath & "/Search_Policies.aspx")
    End Sub

    Private Sub SearchReferral()
        Dim dao As New DAOMain.VW_POLICY_REF_DTL_SEARCH_DAO
        Dim vo As New DAOMain.VW_POLICY_REF_DTL_SEARCH_VO

        With vo
            .CLIENT_NAME = "%" & ClientName_txt.Text & "%"
            .POLICY_NO = "%" & Policy_No.Text & "%"
            .REFERRAL_NAME = "%" & Referralname_txt.Text & "%"
            .STATUS = "%" & Status_txt.Text & "%"
            .ADVISOR_NAME = "%" & Advisor_txt.Text & "%"

            If Not IsNothing(Session("advisor")) Then
                .ADVISOR = Session("UserName") & "%"
            End If
        End With

        Dim dt As DataTable = dao.Search(vo, , True)

        results_label.Text = dt.Rows.Count
        ViewState.Add("mData", dt)

        DataGrid.CurrentPageIndex = 0
        Call BindGrid()

        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
        If e.Item.ItemType = ListItemType.Item Then
            Dim cFunc As New CIMSfunc

            Select Case e.CommandName
                Case "Edit"
                    Session.Add("Seq_CD", e.Item.Cells(0).Text)
                    Session.Add("CID", e.Item.Cells(1).Text)
                    Session.Add("PolicyNo", e.Item.Cells(2).Text)
                    Session.Add("CName", e.Item.Cells(3).Text)
                    Session.Add("Provider", cFunc.GetProvider(e.Item.Cells(0).Text))
                    Session.Add("PageSource", "SearchRefferals")
                    Session.Add("mode", "edit")

                    Response.Redirect("~/" & strPath & "/Add_Referral.aspx")
                Case "View"
                    Session.Add("Seq_CD", e.Item.Cells(0).Text)
                    Session.Add("CID", e.Item.Cells(1).Text)
                    Session.Add("PolicyNo", e.Item.Cells(2).Text)
                    Session.Add("CName", e.Item.Cells(3).Text)
                    Session.Add("Provider", cFunc.GetProvider(e.Item.Cells(0).Text))
                    Session.Add("PageSource", "SearchRefferals")
                    Session.Add("mode", "view")

                    Response.Redirect("~/" & strPath & "/Add_Referral.aspx")
            End Select
        End If
    End Sub

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        If e.NewPageIndex <= DataGrid.PageCount Then
            DataGrid.CurrentPageIndex = e.NewPageIndex
            Call BindGrid()
        End If
    End Sub

    Private Sub BindGrid()
        If Not IsNothing(ViewState("mData")) Then
            DataGrid.DataSource = ViewState("mData")
            DataGrid.DataBind()
        End If
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim crpt As New CReports

        With crpt
            If Not IsNothing(ViewState("mData")) Then
                Dim dt As DataTable = ViewState("mData")

                .ShowRpt("PolicyReferralExport", CReports.RptType.Excel_Report, ParentPage, dt, "vw_policy_ref_dtl_search")
            End If
        End With

        crpt = Nothing
    End Sub
End Class
